﻿using Model;
using Model.TempClass;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Mvc;

namespace FlowMvc.Controllers
{
    public class OASController : Controller
    {
        // GET: OAS
        public ActionResult Index()
        {
            return View();
        }

        /// <summary>
        /// 获取步骤数据 
        /// </summary>
        /// <returns></returns>
        public ActionResult AjaxData()
        {
            int FlowType = 1201;
            using (CFA_OAEntities db = new CFA_OAEntities())
            {
                ////获取当前流程所有的步骤数据
                //List<SysFlowTransfer> listTran = db.SysFlowTransfer.Where(item => item.FlowType == FlowType).Select(item => item).ToList();
                ////获取唯一CurID数据源
                //var listKey = listTran.Where(item => item.FlowType == FlowType).GroupBy(x => new { x.CurID }).ToList();
                ////遍历CurID 并获取transfer详细信息
                //StringBuilder str = new StringBuilder();
                ////str.Append("{");
                //foreach (var item in listKey)
                //{
                //    int? key = item.Key.CurID;
                //    SysFlowTransfer modelTran = listTran.Where(i => i.CurID == key).FirstOrDefault();

                //    FlowModel model = new FlowModel();
                //    model.flow_id = modelTran.FlowType.ToString();
                //    model.icon = modelTran.icon;
                //    model.id = modelTran.CurID.ToString();
                //    model.process_name = modelTran.Name;
                //    model.style = modelTran.style;
                //    string process_to = "";
                //    //获取下一步骤的CurID
                //    foreach (var m in listTran.Where(i => i.CurID == key).ToList())
                //    {
                //        process_to = process_to == "" ? m.ToID + "" : process_to + "," + m.ToID;
                //    }
                //    model.process_to = process_to == "" ? "0" : process_to;
                //    //listFlow.Add(model);
                //    string json = JsonConvert.SerializeObject(model);
                //    if (str.Length > 5)
                //    {
                //        str.Append(",");
                //    }
                //    str.Append("\"" + model.id + "\":" + json);

                //}
                //return Content("{\"list\":{" + str.Replace("null", "\"\"").ToString() + "},\"total\":" + listKey.Count + "}");
            }
            return Content("");
        }
        /// <summary>
        /// 添加节点
        /// </summary>
        /// <param name="Flow"></param>
        /// <returns></returns>
        public ActionResult AddFlow(FlowModel Flow)
        {
            try
            {
                using (CFA_OAEntities db = new CFA_OAEntities())
                {
                    //int FlowType = int.Parse(Flow.flow_id);
                    ////获取当前流程所有的步骤数据
                    //List<SysFlowTransfer> listTran = db.SysFlowTransfer.Where(item => item.FlowType == FlowType).Select(item => item).ToList();
                    //SysFLow FlowModel = db.SysFLow.Where(item => item.Code == FlowType.ToString()).FirstOrDefault();

                    //int? CurID = listTran.Max(i => i.CurID);
                    //int? transferID = listTran.Max(i => i.TransferId);

                    //SysFlowTransfer proModel = new SysFlowTransfer();
                    //proModel.FlowType = FlowType;
                    //proModel.CurID = CurID + 1;
                    //proModel.TransferId = transferID + 1;
                    //proModel.FlowName = FlowModel.Name;
                    //proModel.icon = Flow.icon;
                    //proModel.Name = Flow.process_name;
                    //proModel.style = Flow.style;
                    ////db.SysProcess.Add(proModel);
                    //db.SaveChanges();
                    return Json(new { statu = 1 }, JsonRequestBehavior.AllowGet);
                }
            }
            catch (Exception ex)
            {
                return Json(new { statu = 0 }, JsonRequestBehavior.AllowGet);
            }
        }
        /// <summary>
        /// 删除节点
        /// </summary>
        /// <param name="flow"></param>
        /// <returns></returns>
        public ActionResult DeleteFlow(FlowModel flow)
        {
            try
            {
                using (CFA_OAEntities db = new CFA_OAEntities())
                {
                //    int id = int.Parse(flow.id);
                //    List<SysFlowTransfer> model = db.SysFlowTransfer.Where(item => item.CurID == id).Select(i => i).ToList();
                //    {
                //        db.SysFlowTransfer.RemoveRange(model);
                //    }
                //    db.SaveChanges();
                    return Json(new { statu = 1 }, JsonRequestBehavior.AllowGet);
                }
            }
            catch (Exception ex)
            {
                return Json(new { statu = 0 }, JsonRequestBehavior.AllowGet);
            }
        }
        /// <summary>
        /// 保存设置
        /// </summary>
        /// <param name="FlowInfo"></param>
        /// <returns></returns>
        public ActionResult AjaxSaveProcess(string FlowInfo)
        {
            int? FlowType = 1201;
            try
            {
                List<FlowInfoModel> model = JsonConvert.DeserializeObject<List<FlowInfoModel>>(FlowInfo);
                int result = 0;
                //using (CFA_OAEntities db = new CFA_OAEntities())
                //{
                //    List<SysFlowTransfer> listPro = db.SysFlowTransfer.Where(i => i.FlowType == FlowType).Select(i => i).ToList();
                //    foreach (var item in listPro)
                //    {
                //        var info = model.Where(j => j.process_id == item.CurID).FirstOrDefault();
                //        if (info != null)
                //        {
                //            string styleStr = "";
                //            string[] style = item.style.Split(';');
                //            styleStr += "left:" + info.left + "px;" + "top:" + info.top + "px;";
                //            for (int i = 0; i < style.Length; i++)
                //            {
                //                if (!style[i].Contains("left") && !style[i].Contains("top") && !string.IsNullOrWhiteSpace(style[i]))
                //                {
                //                    styleStr += style[i] + ";";
                //                }
                //            }
                //            item.style = styleStr;

                //            List<SysFlowTransfer> listRelation = db.SysFlowTransfer.Where(i => i.CurID == info.process_id).Select(i => i).ToList();
                //            db.SysFlowTransfer.RemoveRange(listRelation);

                //            for (int i = 0; i < info.process_to.Length; i++)
                //            {
                //                SysRelation relation = new SysRelation();
                //                relation.process_id = info.process_id;
                //                relation.process_toid = info.process_to[i];
                //               // db.SysRelation.Add(relation);
                //            }
                //        }
                //    }
                //    result = db.SaveChanges();
                //}
                if (result > 0)
                {
                    return Json(new { statu = 1 }, JsonRequestBehavior.AllowGet);
                }
            }
            catch (Exception)
            {
                return Json(new { statu = 0 }, JsonRequestBehavior.AllowGet);
            }
            return Json(new { statu = 0 }, JsonRequestBehavior.AllowGet);
        }
    }
}